package com.jk.service;

import com.jk.dao.LoginDao;
import com.jk.entity.*;
import com.jk.utils.Codeutils;
import com.jk.utils.ReturnMsgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class LoginService {

    @Autowired
    private LoginDao loginDao;

   @Autowired
   private MongoTemplate mongoTemplate;

   @Autowired
   private RedisTemplate redisTemplate;

    public Map login(User user, HttpServletRequest request) {
        if (user.getUser_name()==null || "".equals(user.getUser_name())){
            return ReturnMsgUtils.returnMsg(201,"账号为空",null);
        }
        if (user.getUser_password()==null || "".equals(user.getUser_password())){
            return ReturnMsgUtils.returnMsg(202,"密码为空",null);
        }
        if(user.getCode()==null || "".equals(user.getCode())){
            return ReturnMsgUtils.returnMsg(203,"验证码为空",null);

        }else{
              if(!user.getCode().toLowerCase().equals(request.getSession().getAttribute("code").toString().toLowerCase())){
                  return ReturnMsgUtils.returnMsg(204,"验证码错误",null);

              }else{
                    User user1=loginDao.login(user);
                    if(user1==null){
                        return ReturnMsgUtils.returnMsg(205,"账号或者密码错误",null);

                    }else{
                        if(user1.getUser_state()==2){
                            return ReturnMsgUtils.returnMsg(206,"账号冻结,为了你的财产安全问题,请联系客服",null);

                        }else{

                            SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Query query1=new Query();
                            query1.addCriteria(Criteria.where("user_name").is(user.getUser_name()).and("user_password").is(user.getUser_password()));
                            MongUser one1 = mongoTemplate.findOne(query1, MongUser.class);

                            System.out.println(one1);

                            Query query=new Query();
                            query.addCriteria(Criteria.where("id").is(one1.getId()));
                            Update update=new Update();
                            update.set("login_time",sim.format(new Date()));
                            mongoTemplate.upsert(query,update,MongUser.class);
                            request.getSession().setAttribute("user",user1);
                            return ReturnMsgUtils.returnMsg(200,"登录成功",user1);

                        }
                    }

              }


        }
    }

    public Map addUser(User1 user1, HttpServletRequest request) {

        if (user1.getCode() == null || "".equals(user1.getCode())) {
            return ReturnMsgUtils.returnMsg(201, "验证码为空,请重试", null);
        } else {
            if (!user1.getCode().toLowerCase().toString().equals(request.getSession().getAttribute("code").toString().toLowerCase())) {
                return ReturnMsgUtils.returnMsg(202, "验证码错误", null);
            } else {
                User user = new User();
                user.setUser_state(1);
                user.setCount(99);
                user.setUser_type(1);
                user.setUser_email(user1.getEmail());
                user.setUser_name(user1.getUsername());
                user.setInfo("");
                user.setUser_password(user1.getPassword());
                user.setUser_nickname(user1.getNickname());
                user.setPhone(user1.getPhone());
                loginDao.addUser(user);
                MyInfoEntity myInfoEntity=new MyInfoEntity();
                myInfoEntity.setUser_id(user.getUser_id());
                myInfoEntity.setEmail(user1.getEmail());
                myInfoEntity.setPhone(user1.getPhone());
                myInfoEntity.setNickname(user.getUser_nickname());
                myInfoEntity.setName(user1.getUsername());
                loginDao.addMyself(myInfoEntity);
                SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                MongUser mongUser = new MongUser();
                mongUser.setUser_id(user.getUser_id());
                mongUser.setUser_email(user.getUser_email());
                mongUser.setUser_password(user.getUser_password());
                mongUser.setAdd_time(sim.format(new Date()));
                mongUser.setUser_state(1);
                mongUser.setCount(99);
                mongUser.setLogin_time("");
                mongUser.setInfo("");
                mongUser.setUser_type(1);
                mongUser.setUser_nickname(user.getUser_nickname());
                mongUser.setUser_name(user.getUser_name());
                mongUser.setRole_name("");
                mongUser.setPhone(user.getPhone());
                mongoTemplate.save(mongUser);

                return  ReturnMsgUtils.returnMsg(200,"注册成功",null);
            }
        }


    }

    public Map postCode(String phone) {
        System.out.println(phone);
        User login = loginDao.selephone(phone);
        System.out.println(login);
        String code = (String)redisTemplate.opsForValue().get("sendCode"+phone);
        System.out.println(code);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        User user=new User();
        user.setUser_name(phone);
        if(login==null){
        /*    paramMap.put("msg", "该手机号未注册请核对手机号");*/
            return  ReturnMsgUtils.returnMsg(201,"该手机号未注册请核对手机号",null);
        }else{
            if(code != null){
              /*  paramMap.put("code", 200);
                paramMap.put("msg", "当前验证码未过期 稍后尝试");*/
                return  ReturnMsgUtils.returnMsg(202,"当前验证码未过期 稍后尝试",null);
            }else{

             Codeutils.sendMessage(phone, paramMap);
                Integer StatusCode = (Integer)paramMap.get("code");
                if(StatusCode==200){
                    redisTemplate.opsForValue().set("sendCode"+phone, (String)paramMap.get("obj"), 60, TimeUnit.DAYS);
                   /* paramMap.put("msg", "验证码发送成功");*/
                    return  ReturnMsgUtils.returnMsg(200,"验证码发送成功",null);
                }else{
                   /* paramMap.put("msg", "验证码发送失败 请重试");*/
                    return  ReturnMsgUtils.returnMsg(203,"验证码发送失败 请重试",null);
                }
            }
        }
    }

    public Map doLogin(String phone, String code) {
        String vCode =(String) redisTemplate.opsForValue().get("sendCode"+phone);
        User login = loginDao.selephone(phone);
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        User user=new User();
        user.setUser_name(phone);
        if (login==null) {
           /* loginDao.addphone(user);*/
            return  ReturnMsgUtils.returnMsg(201,"不存在当前账号,请重试",null);

        }else {
            if(code.equals(vCode)){
            /*    paramMap.put("msg", "登录成功");
                paramMap.put("user", login);
                paramMap.put("code", 200);*/
                return  ReturnMsgUtils.returnMsg(200,"登陆成功",login);
            }else{
                /*paramMap.put("msg", "验证码错误");*/
                return  ReturnMsgUtils.returnMsg(203,"验证码错误",login);
            }
        }
    }

    public Map checkUserName(String userName) {
        User user=loginDao.checkUserName(userName);
        if(user==null){
            return  ReturnMsgUtils.returnMsg(201,"账号未注册,可使用",null);
        }else{

            return  ReturnMsgUtils.returnMsg(200,"账号已经注册,请重新输入账号",null);
        }

    }

    public MyInfoEntity myInfo(String userName,HttpServletRequest request) {
          User user= (User) request.getSession().getAttribute("user");
          MyInfoEntity my=loginDao.myInfo(user.getUser_id());
          return  my;
    }

    public MyInfoEntity chakan(String userName,HttpServletRequest request) {
        User user= (User) request.getSession().getAttribute("user");
        MyInfoEntity my=loginDao.myInfo(user.getUser_id());
        return  my;
    }

    public void upMyinfo(MyInfoEntity myInfoEntity, HttpServletRequest request) {
        User user= (User) request.getSession().getAttribute("user");
        myInfoEntity.setUser_id(user.getUser_id());
        loginDao.upMyinfo(myInfoEntity);
    }

    public Consignee chakanDing(MyInfoEntity myInfoEntity, HttpServletRequest request) {
        User user= (User) request.getSession().getAttribute("user");
        List<Consignee> list=loginDao.chakanDing(user.getUser_id());
         System.out.println(list);
        Consignee con=new Consignee();
        con.setCon(list);
        System.out.println(con);
        return  con;
    }

    public void shanDi(Integer consignee_id) {
        loginDao.shanDi(consignee_id);
    }

    public void addressMy(Consignee con,HttpServletRequest request) {
        User user= (User) request.getSession().getAttribute("user");
        con.setUser_id(user.getUser_id());
        loginDao.addressMy(con);
    }
}
